{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "match_example: 123\n",
      "search_example: 123\n",
      "findall_example: ['123', '456']\n",
      "finditer_example: 123\n",
      "finditer_example: 456\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "# 匹配从字符串的开头开始的一个或多个数字\n",
    "def match_example():\n",
    "    pattern = re.compile(r'\\d+')\n",
    "    result = pattern.match('123abc456')\n",
    "    if result:\n",
    "        print(\"match_example:\", result.group())\n",
    "    else:\n",
    "        print(\"No match\")\n",
    "\n",
    "# 在字符串中搜索并匹配一个或多个数字\n",
    "def search_example():\n",
    "    pattern = re.compile(r'\\d+')\n",
    "    result = pattern.search('abc123def456')\n",
    "    if result:\n",
    "        print(\"search_example:\", result.group())\n",
    "    else:\n",
    "        print(\"No match\")\n",
    "\n",
    "# 查找字符串中所有匹配的一个或多个数字\n",
    "def findall_example():\n",
    "    pattern = re.compile(r'\\d+')\n",
    "    result = pattern.findall('abc123def456')\n",
    "    print(\"findall_example:\", result)\n",
    "\n",
    "# 使用迭代器查找字符串中的所有匹配项\n",
    "def finditer_example():\n",
    "    pattern = re.compile(r'\\d+')\n",
    "    result_iterator = pattern.finditer('abc123def456')\n",
    "    for result in result_iterator:\n",
    "        print(\"finditer_example:\", result.group())\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    match_example()\n",
    "    search_example()\n",
    "    findall_example()\n",
    "    finditer_example()\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-18T16:14:54.860003800Z",
     "start_time": "2023-12-18T16:14:54.847917300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "456\n",
      "7890\n"
     ]
    }
   ],
   "source": [
    "pattern = re.compile(r'(\\d{3})-(\\d{3})-(\\d{4})')\n",
    "result = pattern.match('123-456-7890')\n",
    "if result:\n",
    "    print(result.group(1))  # 输出第一个分组的内容\n",
    "    print(result.group(2))  # 输出第二个分组的内容\n",
    "    print(result.group(3))  # 输出第三个分组的内容"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-18T16:28:57.600441500Z",
     "start_time": "2023-12-18T16:28:57.585274100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a> <b> <c>\n",
      "<a>\n"
     ]
    }
   ],
   "source": [
    "# 贪婪匹配\n",
    "pattern = re.compile(r'<.*>')\n",
    "text = '<a> <b> <c>'\n",
    "result = pattern.match(text)\n",
    "print(result.group())  # 输出：<a> <b> <c>\n",
    "\n",
    "# 非贪婪匹配\n",
    "pattern = re.compile(r'<.*?>')\n",
    "result = pattern.match(text)\n",
    "print(result.group())  # 输出：<a>\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-18T16:43:56.867610100Z",
     "start_time": "2023-12-18T16:43:56.847630600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello', 'World']\n"
     ]
    }
   ],
   "source": [
    "# 替换字符串中的匹配项\n",
    "new_text = pattern.sub('替换的内容', '原始字符串')\n",
    "\n",
    "# 分割字符串\n",
    "result = re.split(r'\\s', 'Hello World')\n",
    "print(result)  # 输出：['Hello', 'World']\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-18T16:47:20.811429500Z",
     "start_time": "2023-12-18T16:47:20.740333300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "手机号码: ['13812345678', '15098765432']\n",
      "邮箱地址: ['test@example.com']\n",
      "网址: ['https://www.baidu.com。']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "text = \"\"\"\n",
    "这是一段文字，包含手机号码：13812345678和15098765432，还有邮箱地址：test@example.com，以及网址：https://www.baidu.com。\n",
    "\"\"\"\n",
    "\n",
    "phone_pattern = re.compile(r'1[3456789]\\d{9}')\n",
    "email_pattern = re.compile(r'\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b')\n",
    "url_pattern = re.compile(r'https?://\\S+|www\\.\\S+')\n",
    "\n",
    "phone_numbers = phone_pattern.findall(text)\n",
    "emails = email_pattern.findall(text)\n",
    "urls = url_pattern.findall(text)\n",
    "\n",
    "print(\"手机号码:\", phone_numbers)\n",
    "print(\"邮箱地址:\", emails)\n",
    "print(\"网址:\", urls)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-12-18T16:53:36.410143700Z",
     "start_time": "2023-12-18T16:53:36.393686Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
